//
//  CORankMainController.swift
//  CocoProject
//
//  Created by Vill on 2023/11/7.
//

import UIKit

class CORankMainController: COBaseController,UITableViewDelegate,UITableViewDataSource{
    
    var rankModelArr = [CORankModel]()     // 排行数组
    
    lazy var titleLbl: UILabel = {
        let lbl = UILabel()
        lbl.text  = "Ranking"
        lbl.textColor = UIColor.white
        lbl.font = UIFont.pingfangFont(ofSize: PTW(25), weight: .semibold)
        return lbl
    }()

    lazy var baseTableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.backgroundColor = .clear
        tableView.tableFooterView = UIView()
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        if #available(iOS 11.0, *) {
            if #available(iOS 13.0, *) {
                tableView.automaticallyAdjustsScrollIndicatorInsets = false
            }else{
                tableView.contentInsetAdjustmentBehavior = .never
            }
        }else {
            automaticallyAdjustsScrollViewInsets = false
        }
        tableView.tableHeaderView = self.headView
        return tableView
    }()
    
    lazy var headView: CORankHeadView = {
        let head = CORankHeadView(frame: CGRectMake(0, 0, SCREENWIDTH, PTW(220)))
        head.isHidden = true
        head.addTap { [weak self] ges in
            self?.checkTheFirstVc()
        }
        return head
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        kLiveCalling.keySaveToNSDefaults(with: false)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(self.baseTableView)
        view.addSubview(self.titleLbl)
        self.baseTableView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview().offset(KStatusAndNavBarHeight)
            make.bottom.equalToSuperview().offset(-KTabBarAndSafeHeight)
        }
        self.titleLbl.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(PTW(20))
            make.top.equalToSuperview().offset(KStatusBarHeight() + PTW(10))
        }
        self.baseTableView.headerRefresh {[weak self] in
            self?.getRankData()
        }
        self.baseTableView.headerStartRefreshing()
    }
    
    func getRankData(){
        COHttpManager.request(target: COBaseApi.rankSeven, modelType: CORankObjectModel.self) {[weak self]  code, object, success in
            self?.baseTableView.headerEndRefreshing()
            let list = object?.response
            self?.rankModelArr = list ?? [CORankModel]()
            if list?.count ?? 0 > 0{
                CONodataView.dg_hide((self?.baseTableView)!)
            }else {
                CONodataView.dg_show((self?.baseTableView)!)
            }
            if list?.count ?? 0 > 0 {
                if let items = self?.rankModelArr, let list = list{
                    self?.rankModelArr = items + list
                }
            }
            if (self?.rankModelArr.count ?? 0 > 0){
                self?.headView.isHidden = false
                let topmodel = self?.rankModelArr.first
                self?.headView.model = topmodel
            }
            self?.baseTableView.reloadData()
        } failureBlock: {[weak self]  code, msg in
            self?.baseTableView.headerEndRefreshing()
        }
    }
    
    func checkTheFirstVc(){
        let model = self.rankModelArr.first
        let profile = COOtherInfoController()
        profile.userId = model?.streamerId
        self.navigationController?.pushViewController(profile, animated: true)
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.rankModelArr.count - 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = DGNRankTableCell().cellWithTableView(tableView)
        cell.selectionStyle = .none
        let model = self.rankModelArr[indexPath.row + 1]
        cell.model = model
        if(indexPath.row == 0){
            cell.numLbl.isHidden = true
            cell.leftImg.isHidden = false
            cell.leftImg.image = KImaged("coco_rank_second")
        }else if(indexPath.row == 1){
            cell.numLbl.isHidden = true
            cell.leftImg.isHidden = false
            cell.leftImg.image = KImaged("coco_rank_third")
        }else{
            cell.numLbl.isHidden = false
            cell.leftImg.isHidden = true
            cell.numLbl.text = "\(indexPath.row + 2)"
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return PTW(80)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let profile = COOtherInfoController()
        let model = self.rankModelArr[indexPath.row + 1]
        profile.userId = model.streamerId
        self.navigationController?.pushViewController(profile, animated: true)
    }

}
